if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  }
})

function ready() {
    var _thisPage = null, user = null;

    var editnickPage = function () {
        this.init()
    }

    editnickPage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
            if (!user) {
                redirect('login.html');
            }
        },
        bindEvent: function () {

            //点击回退
            $("#back").on("click", function () {
                back();
            });

            //点击清除
            $("#clear").on("click", function () {
                $("#nickname").val("");
            });
            
            $("#nickname").on("input", function(){
              if ("" != $("#nickname").val().trim()){
                $("#saveNick").removeClass("gray_color");
              }else{
                $("#saveNick").addClass("gray_color");
              }
            });

            //点击保存
            $("#saveNick").on("click", function () {
              if($("#saveNick").hasClass("gray_color")){
                return;
              }              
              if(!isNickname($("#nickname").val())) {
                  $.toast("昵称格式不正确");
                  return false;
              } else {
                  $.ajax({
                      type: "PUT",
                      url: CONFIG_MANAGER.RequstUrl + "v1/member/nickname/" + user.id,
                      async: true,
                      data:JSON.stringify(  $("#nickname").val().trim() ),
                      contentType: "application/json; charset=UTF-8",
                      headers: {"token": user.token},
                      beforeSend:function(){ $.showWaiting(); }
                  }).done(function (data) {
                    $.alert("修改成功", function () {
                      redirect("account.html");
                    });
                  }).fail(function (data) {
                      $.toast(data.responseJSON.Message);
                  }).always(function(){
                  	$.closeWaiting();
                  });
              }
            });
        }
    }

    _thisPage = new editnickPage();
    

}
